import { observable, action } from 'mobx';
import { Tools, Toast } from 'weaver-mobile';
import { SearchAdvanced, ListViewPage } from 'weaver-mobile-page';

import * as Api from '../../api/BlogAttentionApi'; 
import * as Main from '../../api/BlogMainApi';
import { PARAMS } from '../../components/public/BlogUtils';
import BlogCommentStore from '../comment/BlogCommentStore';

const { getLabel } = Tools;

export default class BacthAttentionStore {
  @observable loading = true;  // 我的关注-加载中状态
  @observable MyAttentionIsSearch = false; // 标识我的关注页面是否走了搜索
  @observable myAttentionList = new ListViewPage.Store({ route:'/userCenter/blogAttention/attention', id:'myAttention' });  // 我的关注列表
  @observable shareToMeList = new ListViewPage.Store({ route:'/userCenter/shareToMe/shareToMe', id:'shareToMe' });  // 我的关注列表
  @observable showChecked = false; //我的关注 -- 标记批量操作
  @observable myAttentionLoading = true; // 我的关注-列表-加载中状态
  @observable groupSearch = false; // 我的关注--标志是否走了分组查询
  @observable userPage = 1;  // 我的关注--搜索--接口请求页数
  @observable overviewAdValue = ''; // 微博总览-高级搜索值受控 -- 同时用于我的关注搜索值
  @observable overview = new BlogCommentStore({ getComment: this.getOverviewList });  // 微博总览搜索展示
  @observable overviewPage = 1;
  @observable overviewFlag = 0; // 微博总览-标识第一次搜索接口走两次问题

  @observable isSearch = false;

  @observable attentionList = [];   // 关注列表展示信息
  @observable shareToMeUserList = [];   // 关注列表展示信息
  @observable groupData = [];  //Picker Condition展示信息
  @observable countInfos = [];  // 微博总览手风琴日期提示
  @observable workDate = '';
  @observable mygroupid = {groupId:'all',groupName: getLabel(33311, '分享给我')};
 
  @observable currentPage= 1;
  @observable commentStoreMap = {};

  @observable search = new SearchAdvanced.Store({ route: '/userCenter/blogAttention/attention', id: 'adSearch' }); // 高级搜索

  @observable type="";
  @action setStoreState = (params) => {
    Object.keys(params).map(key => {
      this[key] = params[key];
    })
  }


  @action
  setType = (type) => {
    this.type = type;
  }


  /************************************* 我的关注 *************************************/
  // 初始化我的关注页面
  initalMyAttention = () => {
    this.myAttentionLoading = true;
    this.blogSearchCondition();
    //this.getMyAttentionGroup();
    this.initalShareToMeList({groupId:'all'});
  }

    @action changeMyGroup = (parms) => {
        this.mygroupid=parms;
    }

  @action('我的关注--展示列表')
  initalShareToMeList = (params) => {
    this.showChecked = false;

    Api.getShareToMeList(this.mygroupid).then(res => {
      const { status, shareToMeUserList } = res;
      if (status === '1') {
        const newState = {
          isTable: false,
          loading: false,
          haveCheck: true,
          template: {default: []},
          hasMore: false,
          data: [...shareToMeUserList],
        };
 
        

        this.shareToMeList.setState(newState);
        this.shareToMeList.updateDataSource();
      }
      this.loading = false;
      this.myAttentionLoading = false;
    })
  }

  @action('我的关注--搜索-列表 && 搜索-上拉刷新')
  myAttentionSearch = () => {
    if (this.overviewAdValue === '' || !this.overviewAdValue) {
      this.MyAttentionIsSearch = false;
      this.initalShareToMeList({groupId:'all'})
    }else {
      this.MyAttentionIsSearch = true;
      this.userPage = 1;
      this.getMyAttentionDatas();
    }
  }

  @action('我的关注--搜索-获取下一页数据')
  myAttentionSearchNextPage = () => {
    this.getMyAttentionDatas();
  }

  @action('我的关注-搜索接口数据返回')
  getMyAttentionDatas = (params) => {
    this.shareToMeList.setState({loading: true});
    params = {
        searchKey: this.overviewAdValue
    //   currentPage: this.userPage++,
    //   pageSize: 30,
    } 
    Api.getShareToMeList(params).then(res => {
      const {status, shareToMeUserList} = res;
      if (status === '1') {
        const newState = {
            isTable: false,
            loading: false,
            haveCheck: true,
            template: {default: []},
            hasMore: false,
            data: [...shareToMeUserList],
          };
        
        this.shareToMeList.setState(newState);
        this.shareToMeList.updateDataSource();
        this.myAttentionLoading = false;
      }
    })
  }
  
  
  @action('批量关注')
  batchAttention = (islowers,userIds) => {
    let params = {islower:islowers,userId:userIds}
    Api.batchAttentionOpt(params)
    .then(result => {
      if(result.status=='1'){
        Toast.info(getLabel('30700','操作成功') , 1);
        result.attnInfo.forEach((item, key) => {
        this.shareToMeList.data.forEach((item1, key1) => {
            if(item.userId ==item1.userId ){
                this.shareToMeList.data[key1] = item;
            }
        });
      });
        this.shareToMeList.selectNone();
      }
    })
  }

  @action('我的关注--取消')
  myAttentionCancel = () => {
    this.showChecked = false;
    this.myAttentionSelectNone();
  }

  // 我的关注--全选
  @action myAttentionSelectAll = () => this.shareToMeList.selectAll();

  // 我的关注--取消全选
  @action myAttentionSelectNone = () => this.shareToMeList.selectNone();

  @action('我的关注--更新关注状态')
  updateUserAttentionStatus = (attnInfo) =>{
    let {userId,attentionStatus} = attnInfo;
		let attlist = this.shareToMeList.data.map(att=>{
      if (att.userId == userId)
        return {...att, attentionStatus: attentionStatus};
      return att;
		});
    this.shareToMeList.setState({data: attlist});
    this.shareToMeList.updateDataSource();
  }

  @action('分组信息')
  getMyAttentionGroup = () => {
    Api.getMyAttentionGroup().then(data=>{
      this.groupData = data.groupList;
    })
  }

  @action('移动到组')
  moveContactToBlogGroup = (params, callback) => {
    Api.moveContactToBlogGroup(params).then(result => {
      if (result.status == 1) {
        this.getMyAttentionGroup();
        if(callback) callback();
      }
    })
  }

  @action('复制到组') 
  copyContactToBlogGroup = (params, callback) => {
    Api.copyContactToBlogGroup(params).then(result => {
      if (result.status == 1) {
        this.getMyAttentionGroup();
        if(callback) callback();
      }
    })
  }

  @action('批量取消关注') 
  batchCancelAttn = (params, callback) => {
    Api.batchCancelAttn(params).then(result => {
      if (result.status == 1) {
        if(callback) callback();
      }
    })
  }

  /* **********************  微博总览  ********************** */
  @action('微博总览Comment组件显示')
  getCommentStore = (key) => {
    if (!this.commentStoreMap[key]) {
      this.commentStoreMap[key] = new BlogCommentStore({ getComment: this.getOverviewList });
    }
    return this.commentStoreMap[key];
  }

  // 高级搜索Condition加载
  blogSearchCondition = () => {
    this.search.getData({
      url:`/api/blog/base/blogSearchCondition`,
      method: `GET`,
      params: {...PARAMS, listType: 'homepage'}
    })
  }

  /* 高级搜索关联操作 */
  @action('关联高级搜索与前端搜索条件')
  updateAdSearchSubject = (value) => {
    this.overviewAdValue = value;
    this.search.form.updateField('content', value);
  }
 
  @action('微博总览Comment请求接口') 
  getOverviewList = () => {
    if (this.isSearch) {  // 微博总览搜索状态请求接口
      let overViewParams = this.search.form.formParams || {};
      let params = {
        pageSize: 15,
        currentPage: this.overviewPage++,
        ...overViewParams
      }
      return Main.getBlogHomepageListBySearch(params).then(data => {
        if(data.pageSize > data.replyList.length) {
          let data_params = {hasMore: false}
          Object.assign(data, data_params)
        }
        return data
      })
    } else {
      let params = {
        workDate: this.workDate,
      }
      return Api.getAttentionWorkDateList(params).then(data => {
        let data_params = {hasMore: false}
        Object.assign(data, data_params)
        return data
      })
    }
  }

  @action('微博总览手风琴日期提示信息')
  getAttentionCountInfos = (params) => {
    Api.getAttentionCountInfos(params).then(data=>{
      this.countInfos = data.countInfos;
    })
  }

  @action('微博总览搜索查询')
  overViewSearch = (callback) => {
    if(this.search.form && !this.search.form.formParams.content){
      this.isSearch = false;
      this.getAttentionCountInfos(this.search.form.formParams);
    }else{
      this.isSearch = true;
      this.overviewPage = 1;
      if(this.overviewFlag !== 1) this.overview.blogCommentStore.getDatas();
    }
    if (callback) callback();
  }

  @action('微博总览提醒全部')
  sendRemindUnsubmitAll = (val) => {
    let params = {
      workdate: val
    }
    Api.sendRemindUnsubmitAll(params).then(data=>{
      if(data.status==='1'){
        Toast.info(getLabel(391235,'已全部提醒'), 1);
      }
    })
  }

  /* 我的关注组件卸载时操作 */
  clearMyAttention = () => {
    this.overviewAdValue = '';
    this.overviewPage = 1;
    this.search.form.resetFields();
    this.isSearch = false;
    this.showChecked = false;
    this.myAttentionSelectNone();
    this.groupSearch = false;
  }

  // 清空数组
  clearState = (callback) => {
    this.attentionList = [];
    this.currentPage= 1;
    if(callback)  callback()
  }

  /********************* 工具关注操作 *********************/
  @action('关注操作')
  attentionOpt = (params, callback) => {
    Api.attentionOpt(params).then(result => {
      if (result.status == 1) {
        Toast.info(getLabel('30700','操作成功') , 1);
        if (callback) callback(result.attnInfo);
      }
    })
  }
}